#!/bin/bash

ffsystem()
{
    ffrun ffuser "add new administrator"
    ffrun ffsources "change your apt source"
    ffrun ffupdate "update your system"
    ffrun ffhostname "change your hostname"
    ffrun fftime "adjust your time"
}

ffuser()
{
    ffbackup "/etc/passwd /etc/shadow"
    ffecho notice "input your administrator username,if you don't want to add,please input enter directly"
    ffread fcusername
    if [ ! -z "$fcusername" ]; then
        fcisuser=$(cat /etc/passwd | grep $fcusername)
        if [ -z "$fcisuser" ]; then
            useradd $fcusername -m -d /home/$fcusername -s /bin/bash -g sudo
            ffecho notice "please input your password:"
            passwd $fcusername
            export $fcusername
        else
            ffecho notice "User $fcusername has existed, We will try again"
            ffuser
        fi
    else
        ffecho notice "you havn't add any new administrator"
    fi
}


ffsources()
{
    ffbackup "/etc/apt/sources.list"
    cp $fcroot/conf/sources.list /etc/apt/sources.list
}


ffupdate()
{
    ffecho notice "we will begin update system,it will takes some time"
    apt-get update
    apt-get dist-upgrade -y
}


ffhostname()
{
    ffbackup "/etc/hosts /etc/hostname"
    ffecho notice "Please input your new hostname,if don't want to change,please enter directly"
    ffread fchostname
    fcoldhostname=$(hostname)
    if [ "$fchostname" = "$fcoldhostname" ]; then
        ffecho notice "Your name was unchanged"
        ffhostname
    elif [ -z "$fchostname" ]; then
        ffecho good "You havn't change your hostname"
    else
        echo $fchostname > /etc/hostname
        hostname $fchostname
        sed -i "s/$fcoldhostname/$fchostname/g" /etc/hosts 
    fi
}


fftime()
{
    ffbackup /etc/crontab
    ntpdate ntp.sjtu.edu.cn
    sed -i -e /ntpdate/d /etc/crontab
    echo '2 */1    * * *   root    /usr/sbin/ntpdate ntp.sjtu.edu.cn' >> /etc/crontab
    /etc/init.d/cron restart > /dev/null
    /etc/init.d/cron start > /dev/null
}

ffmaxfile()
{
    ffbackup "/etc/security/limits.conf /etc/pam.d/common-session"
    ulimit -n 65535
    echo "*     -   nofile  65535" >> /etc/security/limits.conf
    echo "root  -   nofile  65535" >> /etc/security/limits.conf
    echo "session   required    pam_limits.so" >> /etc/pam.d/common-session
    echo "ulimit -SHn 65535"
}


ffmotd()
{
    ffbackup "/etc/update-motd.d /etc/motd.tail"
    ffrm "/etc/update-motd.d/00-header /etc/update-motd.d/10-help-text"
    ffrm "/etc/update-motd.d/50-landscape-sysinfo /etc/update-motd.d/90-updates-available /etc/update-motd.d/91-release-upgrade"
    cp $fcroot/conf/motd.tail /etc/
    run-parts /etc/update-motd.d > /dev/null
}
